package com.hontech.bread.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.hontech.bread.R
import com.hontech.bread.device.DeviceStatus
import com.hontech.bread.ui.view.CargoState
import com.hontech.bread.ui.view.LabelTextView
import com.hontech.bread.utils.isBitSet
import com.hontech.bread.utils.registerEvent
import com.hontech.bread.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class Status2Fragment : Fragment() {

    private lateinit var wrap: StatusWrap

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_status2, null)
        initUi(view)
        return view
    }

    private fun initUi(view: View) {
        wrap = StatusWrap(view)
    }

    override fun onStart() {
        super.onStart()
        registerEvent(this)
    }

    override fun onStop() {
        super.onStop()
        unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onStatusUpdate(status: DeviceStatus) {
        wrap.update(status)
    }
}

private class StatusWrap(view: View) {

    private class SensorWrap(
        val view: CargoState,
        val bit: Int
    )

    private val adc = view.findViewById<LabelTextView>(R.id.id_s2_lt_adc)
    private val temp = view.findViewById<LabelTextView>(R.id.id_s2_lt_temp)

    private val sn = arrayOf<SensorWrap>(
        SensorWrap(view.findViewById(R.id.id_s2_cs_s0), 0),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s1), 2),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s2), 3),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s3), 6),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s4), 4),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s5), 7),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s6), 11),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s7), 9),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s8), 10),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s9), 1),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s10), 13),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s11), 14),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s12), 8),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s13), 12),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s14), 19),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s15), 20),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s16), 22),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s17), 23),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s18), 25),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s19), 24),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s20), 26),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s21), 28),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s22), 18),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s23), 15),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s24), 16),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s25), 17),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s26), 21),
        SensorWrap(view.findViewById(R.id.id_s2_cs_s27), 30)
    )

    fun update(status: DeviceStatus) {
        adc.set(status.adc)
        temp.set(status.pt100)

        for (s in sn) {
            s.view.setState(status.swSensor.isBitSet(s.bit))
        }
    }
}






